//
//  FPPushSettingController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/3/14.
//

#import "FPPushSettingController.h"
#import "FPPushSwitchModel.h"
#import "FPPushSettingView.h"

@interface FPPushSettingController ()

@property (nonatomic , assign) BOOL openPush; //!< 总开关

@property (nonatomic , strong) FPPushSettingView *view1;
@property (nonatomic , strong) FPPushSettingView *view2;
@property (nonatomic , strong) FPPushSettingView *view3;
@property (nonatomic , strong) FPPushSettingView *view4;

@end

@implementation FPPushSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)initUI {
    
    self.openPush = YES;
    
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    [self setTitle:@"推送设置" style:(XYCustomNavigationStyleWhite) hiddenBackBtn:NO];
    
    FPPushSwitchModel *m1 = [[FPPushSwitchModel alloc] init];
    m1.title = @"推送开关";
    m1.subTitle = @"开启推送后，系统将为您推送相关消息";
    
    FPPushSwitchModel *m2 = [[FPPushSwitchModel alloc] init];
    m2.title = @"账务提醒";
    m2.subTitle = @"为您推送交易、还款等财务信息";
    
    FPPushSwitchModel *m3 = [[FPPushSwitchModel alloc] init];
    m3.title = @"服务通知";
    m3.subTitle = @"为您推送状态变更等服务消息";
    
    FPPushSwitchModel *m4 = [[FPPushSwitchModel alloc] init];
    m4.title = @"福利活动";
    m4.subTitle = @"为您推送福利、优惠等活动消息";
    
    
    self.view1 = [self getSettingViewWithModel:m1 y:UIDevice.navigationFullHeight + 40.f index:0];
    self.view1.lineView.hidden = YES;
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16.f, self.view1.bottom + 20.f, 200.f, 20.f)];
    lab.text = @"消息类型";
    lab.font = FONT(12.f);
    lab.textColor = [UIColor hexColor:@"#9A9A9A"];
    
    self.view2 = [self getSettingViewWithModel:m2 y:lab.bottom + 10.f index:1];
    self.view3 = [self getSettingViewWithModel:m3 y:self.view2.bottom index:2];
    self.view4 = [self getSettingViewWithModel:m4 y:self.view3.bottom index:3];
    self.view4.lineView.hidden = YES;
    self.view1.backView.layer.cornerRadius = 5.f;
    [UIView setCornerForView:self.view2 withRadius:5.f bounds:CGRectMake(16.f, 0.f, SCREEN_WIDTH - 32.f, 65.f) corners:(UIRectCornerTopLeft | UIRectCornerTopRight)];
    [UIView setCornerForView:self.view4 withRadius:5.f bounds:CGRectMake(16.f, 0.f, SCREEN_WIDTH - 32.f, 65.f) corners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)];
    
    [self.view addSubview:lab];
    [self.view addSubview:self.view1];
    [self.view addSubview:self.view2];
    [self.view addSubview:self.view3];
    [self.view addSubview:self.view4];
}


- (FPPushSettingView *)getSettingViewWithModel:(FPPushSwitchModel *)model y:(CGFloat)y index:(NSUInteger)index {
    
    FPPushSettingView *view = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPPushSettingView.class) owner:nil options:nil].lastObject;
    
    view.frame = CGRectMake(0.f, y, SCREEN_WIDTH, 65.f);
    view.titleLab.text = model.title;
    view.subTitleLab.text = model.subTitle;
    [view.pushSwitch setOn:model.openPush];
//    view.pushSwitch.indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    view.pushSwitch.tag = 1000 + index;
    [view.pushSwitch addTarget:self action:@selector(getPushSwitchData:) forControlEvents:(UIControlEventValueChanged)];
    
    return view;
}




- (void)getPushSwitchData:(FPCustomSwitch *)sender {
    
    if (sender.tag == 1000) {
        
        self.openPush = sender.isOn;
        
        [self.view2.pushSwitch setOn:self.openPush animated:YES];
        [self.view3.pushSwitch setOn:self.openPush animated:YES];
        [self.view4.pushSwitch setOn:self.openPush animated:YES];
    }
    else {
        
        if (self.view2.pushSwitch.isOn && self.view3.pushSwitch.isOn && self.view4.pushSwitch.isOn) {
            
            self.openPush = YES;
            [self.view1.pushSwitch setOn:self.openPush animated:YES];
        }
        else if (!self.view2.pushSwitch.isOn && !self.view3.pushSwitch.isOn && !self.view4.pushSwitch.isOn) {
            
            self.openPush = NO;
            [self.view1.pushSwitch setOn:self.openPush animated:YES];
        }
        else if (self.view2.pushSwitch.isOn || self.view3.pushSwitch.isOn || self.view4.pushSwitch.isOn) {
            
            self.openPush = YES;
            [self.view1.pushSwitch setOn:self.openPush animated:YES];
        }
    }
}




@end
